package testJboss2;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class UserManagerBean implements UserManager {
	@PersistenceContext(unitName="testJboss2Persist")
	EntityManager em;
	
	@Override
	public User addUser(String firstName, String lastName, Email email, List<Phone> phones){
		
		User u = new User(firstName, lastName);
		if(email!=null){
			u.setDefaultEmail(email);
		}
		if(phones!=null){
			u.getPhones().addAll(phones);
		}
		
		em.persist(u);
		
		return u;
	}
	
	@Override
	public void deleteUser(int userId){
		User u = em.find(User.class, userId);
		//Remove phones.
		//for(Phone phone : )
		List<Phone> phones = u.getPhones();
		for(Phone phone : phones){
			System.out.println(phone.getNumber());
		}
	}
	
	
	@Override
	public void setEmail(User u, String address){
		u.setDefaultEmail(new Email(address));
	}
	
	
}
